<template>
  <div>
    <h1 style="margin:25px 25px">新增专家信息</h1>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="专家姓名" prop="expertName">
        <el-input v-model="formData.expertName" placeholder="请输入专家姓名" :maxlength="200" show-word-limit
          clearable prefix-icon='el-icon-mobile' :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="头像上传" prop="expertAvatar">
        <el-upload 
        ref="expertAvatar" 
        :file-list="expertAvatarfileList" 
        :action="expertAvatarAction"
        :on-success="setFileUrl"
        :headers="headers"
        :limit="1"
        :before-upload="expertAvatarBeforeUpload" accept="image/*">
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传不超过 2MB 的image/*文件</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="学院" prop="college">
        <el-input v-model="formData.college" placeholder="请输入学院学院学院学院" :maxlength="200" show-word-limit
          clearable :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="职务" prop="job">
        <el-input v-model="formData.job" placeholder="请输入职务" :maxlength="200" show-word-limit clearable
          :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" :maxlength="200" show-word-limit clearable
          :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="个人信息" prop="personInfo">
        <el-input v-model="formData.personInfo" type="textarea" placeholder="请输入个人信息" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="社会兼职" prop="socialPartJob">
        <el-input v-model="formData.socialPartJob" placeholder="请输入社会兼职" :maxlength="200" show-word-limit
          clearable :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="研究方向" prop="researchDir">
        <el-input v-model="formData.researchDir" type="textarea" placeholder="请输入研究方向" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="教育背景" prop="edbg">
        <el-input v-model="formData.edbg" type="textarea" placeholder="请输入教育背景" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="工作经历" prop="workExp">
        <el-input v-model="formData.workExp" type="textarea" placeholder="请输入工作经历" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="获奖信息" prop="awardInfo">
        <el-input v-model="formData.awardInfo" type="textarea" placeholder="请输入获奖信息" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="授课课程" prop="teachCour">
        <el-input v-model="formData.teachCour" type="textarea" placeholder="请输入授课课程" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="发表论文" prop="article">
        <el-input v-model="formData.article" type="textarea" placeholder="请输入发表论文" show-word-limit
          :autosize="{minRows: 4, maxRows: 400}" :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {addExpert} from "@/api/tcsp/expert";
import { getToken } from "@/utils/auth";
export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        expertName: '',
        expertAvatar: null,
        college: undefined,
        job: undefined,
        email: undefined,
        personInfo: undefined,
        socialPartJob: undefined,
        researchDir: undefined,
        edbg: undefined,
        workExp: undefined,
        awardInfo: undefined,
        teachCour: undefined,
        article: undefined,
        
      },
      rules: {
        expertName: [{
          required: true,
          message: '请输入专家姓名',
          trigger: 'blur'
        }],
        college: [{
          required: true,
          message: '请输入学院学院学院学院',
          trigger: 'blur'
        }],
        job: [{
          required: true,
          message: '请输入职务',
          trigger: 'blur'
        }],
        email: [{
          required: true,
          message: '请输入邮箱',
          trigger: 'blur'
        }, {
          pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
          message: '邮箱格式错误',
          trigger: 'blur'
        }],
        personInfo: [{
          required: true,
          message: '请输入个人信息',
          trigger: 'blur'
        }],
        socialPartJob: [],
        researchDir: [],
        edbg: [],
        workExp: [],
        awardInfo: [],
        teachCour: [],
        article: [],
      },
      expertAvatarAction: 'http://localhost:8080/minio/upload',
      expertAvatarfileList: [],
      headers: {
          Authorization: "Bearer " + getToken(),
      },
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
            // TODO 提交表单
          addExpert(this.formData).then(resp => {
            this.$modal.msgSuccess("新增成功");
          })
        }
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields();
      this.expertAvatarfileList=[];
    },
    expertAvatarBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      let isAccept = new RegExp('image/*').test(file.type)
      if (!isAccept) {
        this.$message.error('应该选择image/*类型的文件')
      }
      return isRightSize && isAccept
    },
    setFileUrl(res,file){
      //上传成功
      this.formData.expertAvatar= file.name
    }
  }
}

</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
